//
//  YDFundDetailsTableHeaderView.m
//  PeiZiWangBei
//
//  Created by LK-IOS on 2017/11/14.
//  Copyright © 2017年 LK-IOS. All rights reserved.
//

#import "YDFundDetailsTableHeaderView.h"


@interface YDFundDetailsTableHeaderView()
//
@property (nonatomic , weak) UIView *buttonsView;

//保存上一次选中的按钮
@property (nonatomic , weak) SYNoHeightedButton *previousSelectedButton;

@property (nonatomic , weak) UIView *lineView;

@end

@implementation YDFundDetailsTableHeaderView

+ (instancetype)headerViewWithTableView:(UITableView *)tableView
{
    
    static NSString *fundDetailsTableHeaderView = @"YDFundDetailsTableHeaderView";
    YDFundDetailsTableHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:fundDetailsTableHeaderView];
    if (headerView == nil) {
        headerView = [[YDFundDetailsTableHeaderView alloc] initWithReuseIdentifier:fundDetailsTableHeaderView];
    }
    return headerView;
}


-(instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithReuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.contentView.backgroundColor = [UIColor whiteColor];
    
        [self buttonsView];
        
        [self addButtons];
        
        [self lineView];
        
    }
    return self;
}

-(UIView *)buttonsView
{
    if (_buttonsView == nil) {
        UIView *buttonsView = [[UIView alloc] init];
        [self.contentView addSubview:buttonsView];
        buttonsView.backgroundColor = [UIColor whiteColor];
        [buttonsView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.top.equalTo(self);
        }];
        
        _buttonsView = buttonsView;
    }
    return _buttonsView;
}

//添加按钮
-(void)addButtons
{
    NSArray *buttonTitlesArray = @[@"全部" , @"最近一周" , @"最近一月" , @"最近半年"];
    CGFloat buttonW = LKScreenW * 0.25;
    CGFloat buttonH;
    if (isPhoneX) {
        buttonH = rateX812(50);
    }else{
        buttonH = rate750(50);
    }
    for (int i = 0; i < buttonTitlesArray.count; i ++) {
        SYNoHeightedButton *button = [SYNoHeightedButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(i * buttonW, 0, buttonW, buttonH);
        [self.buttonsView addSubview:button];
        button.tag = i;
        if (i == 0) {
            button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
            button.selected = YES;
            self.previousSelectedButton = button;
        }else{
            button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 5);
        }
        [button setTitle:buttonTitlesArray[i] forState:UIControlStateNormal];
        [button setTitleColor:NumberColor(129, 128, 129) forState:UIControlStateNormal];
        [button setTitleColor:[UIColor colorWithHexString:dark] forState:UIControlStateSelected];
        [button setImage:[UIImage imageNamed:@"wd-zijinmingxi-weixuanzhong"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"wd-zijinmingxi-dianxuan"] forState:UIControlStateSelected];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchDown];
        button.titleLabel.font = [UIFont systemFontOfSize:font14ScreenWRate * LKScreenW];
    }
    

}

-(void)buttonClick:(SYNoHeightedButton *)button
{
    if (button.selected)return;
    button.selected = YES;
    self.previousSelectedButton.selected = NO;
    self.previousSelectedButton = button;

    
    if ([self.delegate respondsToSelector:@selector(fundDetailsTableHeaderView:didClickButtonIndex:)]) {
        [self.delegate fundDetailsTableHeaderView:self didClickButtonIndex:button.tag];
    }
    
}

-(UIView *)lineView
{
    if (_lineView == nil) {
        UIView *lineView = [[UIView alloc] init];
        [self addSubview:lineView];
        lineView.backgroundColor = NumberColor(247, 247, 247);
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self);
            make.height.equalTo(@1);
        }];
        _lineView = lineView;
    }
    return _lineView;
}

@end
